﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

public partial class WebUserControl_ControlUpdateInvoDateNotClosed : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                GetTTPP();
                GetMinDate(DropTTPP.SelectedValue.ToString());
            }
            catch (Exception ex)
            {
                BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
                string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
                obj.ErrorLog(ex.Message, method);
            }
        }
    }
    protected void btMove_Click(object sender, EventArgs e)
    {
        try
        {
            dt = new ClassExecute.ClassExecute();

            System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
            sFormat.ShortDatePattern = "dd/MM/yyyy";
            DateTime dateSelect = Convert.ToDateTime(txtNgayHoaDon.Text.Trim(), sFormat);

            DateTime dateMindate = (DateTime)Session["MinDate"];
            if (dateSelect < dateMindate)
            {
                uscMsgBox1.AddMessage("Ngày được chọn nhỏ hơn ngày đã khóa sổ,đề nghị chọn lại", uscMsgBox.enmMessageType.Error);

            }
            else
            {
                int y = 0;
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox chkDelete = (CheckBox)GridView1.Rows[i].FindControl("CheckItem");
                    if (chkDelete.Checked)
                    {
                        bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_updateInvDate]", ref error,
                            new SqlParameter("@invNo", GridView1.Rows[i].Cells[0].Text),
                            new SqlParameter("@selDate", txtNgayHoaDon.Text.Trim()),
                            new SqlParameter("@userID", Session["username"].ToString()));
                        y++;
                    }
                }
                if (y == 0)
                {
                    uscMsgBox1.AddMessage("Bạn chưa chọn hóa đơn nào để chuyển", uscMsgBox.enmMessageType.Error);
                }
                else
                {
                    uscMsgBox1.AddMessage("Nghiệp vụ chuyển thành công", uscMsgBox.enmMessageType.Success);
                    GetMinDate(DropTTPP.SelectedValue.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    void GetTTPP()
    {

        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getDistributorList]", CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
    protected void ImgRequest_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GetMinDate(DropTTPP.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void DropTTPP_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GetMinDate(DropTTPP.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    void GetMinDate(string TTPP)
    {
        
        dt = new ClassExecute.ClassExecute();
        
            string closedDate = dt.GetDataDepend(str, "[ls_getClosedDate]", CommandType.StoredProcedure, ref error,
              new SqlParameter("@siteCode", TTPP));
            DateTime date;
            System.Globalization.DateTimeFormatInfo sFormat = new System.Globalization.DateTimeFormatInfo();
            sFormat.ShortDatePattern = "dd/MM/yyyy";
            date = Convert.ToDateTime(closedDate, sFormat);
            Session["MinDate"] = date.AddDays(1);
            txtNgayHoaDon.Text = date.AddDays(1).ToString("dd/MM/yyyy");

            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getUnPostInvoices]", CommandType.StoredProcedure,
                new SqlParameter("@siteCode", TTPP));
            if (db.Rows.Count > 0)
            {
                GridView1.DataSource = db;
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        
    }
}
